/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.security.KeyStoreException;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

import javax.swing.JScrollPane;
import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.plugin.portunis.Utils;
import nu.zoom.swing.desktop.plugin.portunis.ZoomKeyStore;

import org.ops4j.gaderian.Messages;

/**
 * A component that displays a list of certificates in a keystore. The user can
 * select a certificate alias to see more information about the certificate.
 * 
 * @see nu.zoom.swing.desktop.plugin.portunis.impl.CertificateComponentFactory
 * @version $Revision: 1.3 $
 * @author $Author: johan $
 */
public class DisplayCertificates extends JComponent {

    private static final long serialVersionUID = -1;
    private final CertificateComponentFactory certComponentFactory;
    private Component currentCert = null;

    public DisplayCertificates(final Workbench workbench, final ZoomKeyStore store,
            final ArrayList<String> certAliases, final Messages messages) throws KeyStoreException {
        super();
        certComponentFactory = new CertificateComponentFactory(workbench,
                messages);
        Box certButtonBox = Box.createVerticalBox();
        for (Iterator<String> certIter = certAliases.iterator(); certIter.hasNext();) {
            String alias = certIter.next();
            Certificate[] chain = store.getCertificateChain(alias);
            if (chain != null) {
                // TODO Cert chain
//				JButton aliasButton = new JButton(alias, Utils
//						.getIconFromMessages(messages,
//								"keystore.display.cert.chain.icon"));
            } else {
                Certificate trustedRoot = store.getCertificate(alias);
                JButton aliasButton = new JButton(alias, Utils.getIconFromMessages(messages,
                        "keystore.display.cert.icon"));
                aliasButton.addActionListener(new DisplayCertificateAction(
                        workbench, trustedRoot));
                certButtonBox.add(aliasButton);
            }
        }
        setLayout(new BorderLayout());
        add(new JScrollPane(certButtonBox), BorderLayout.WEST);
        currentCert = new JLabel(messages.getMessage("keystore.display.cert.selectone"));
        add(currentCert, BorderLayout.CENTER);
    }

    void setDetails(JComponent component) {
        if (currentCert != null) {
            remove(currentCert);
        }
        add(component, BorderLayout.CENTER);
        currentCert = component;
        revalidate();
    }

    class DisplayCertificateAction extends AbstractAction {

        private final static long serialVersionUID = -1;
        private JComponent certificateDetailsComponent = null;
        private Certificate cert;

        public DisplayCertificateAction(Workbench workbench, Certificate cert) {
            this.cert = cert;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (certificateDetailsComponent == null) {

                certificateDetailsComponent = new JScrollPane(certComponentFactory.getCertificateComponent(cert));
            }

            setDetails(certificateDetailsComponent);
        }
    }
}
